<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="就诊人姓名">
          <el-input v-model.trim="searchForm.visitor"></el-input>
        </el-form-item>

        <!-- <el-form-item label="年龄">
          <el-select v-model="searchForm.age" placeholder="请选择">
            <el-option v-for="item in ageList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="服务项目">
          <el-input v-model.trim="searchForm.proDes"></el-input>
        </el-form-item>
        <el-form-item label="护士姓名">
          <el-input v-model.trim="searchForm.dispatchNurses"></el-input>
        </el-form-item>
        <el-form-item label="护士手机号">
          <el-input v-model="searchForm.nursesPhone" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchForm.status" placeholder="请选择">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input v-model.trim="searchForm.orderNum"></el-input>
        </el-form-item>

        <el-form-item label="医院名称">
          <el-input v-model="searchForm.hospitalName" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="下单时间">
          <el-date-picker v-model="searchForm.orderTime" value-format="yyyy-MM-dd" type="daterange" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务时间">
          <el-date-picker v-model="searchForm.serviceTime" value-format="yyyy-MM-dd" type="daterange" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="联系人电话">
          <el-input v-model="searchForm.contactPhone" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <!-- <el-button type="primary" @click="showAdd" class="add-button">添加</el-button> -->
          <el-button @click="derive" class="search-button">导出</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="table" v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="医院" align="center" prop="hospitalName">
        </el-table-column>
        <el-table-column label="订单编号" align="center" prop="orderNum">
        </el-table-column>
        <el-table-column label="就诊人" align="center" prop="visitor">
        </el-table-column>
        <el-table-column label="性别" align="center" prop="sex">
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age">
        </el-table-column>
        <el-table-column label="就诊人地址" align="center" prop="visitorAddress">
        </el-table-column>
        <el-table-column label="服务项目" align="center" prop="serviceItem">
        </el-table-column>
        <el-table-column label="派发护士" align="center" prop="dispatchNurses">
        </el-table-column>
        <el-table-column label="护士手机号" align="center" prop="nursesPhone">
        </el-table-column>
        <el-table-column label="下单时间" align="center" prop="orderTiem">
        </el-table-column>
        <el-table-column label="出发时间" align="center" prop="setOutTime">
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
        </el-table-column>
        <el-table-column label="服务结束时间" align="center" prop="serviceFinishTime">
        </el-table-column>
        <el-table-column label="联系人姓名" align="center" prop="contactName">
        </el-table-column>
        <el-table-column label="联系人电话" align="center" prop="contactPhone">
        </el-table-column>
        <el-table-column align="center" label="操作" width="160">
          <template slot-scope="{row}">
            <el-button type="text" @click="goDetails(row)">详情</el-button>
            <el-button type="text" @click="firstAssess(row)" v-if="row.status === '待评估'">首次评估</el-button>
            <el-button type="text" @click="showNurse(row)" v-if="row.status === '待派发'">派发护士</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog title="首次评估" :visible.sync="firstAssessDialog" width="60%">
      <firstAssessTemp v-if="firstAssessDialog" @close="closeFirstAssess" :uid="evaluateUid"></firstAssessTemp>
    </el-dialog>
    <el-dialog title="派发护士" :visible.sync="nurseDialog" width="80%">
      <nurseList v-if="nurseDialog" @close="closeNurse"></nurseList>
    </el-dialog>
  </div>
</template>

<script>
import * as orderManagement from '@/api/orderManagement'
import Pagination from '@/components/Pagination'
import firstAssessTemp from '@/components/firstAssessTemp'//
import nurseList from '@/components/nurseList'
export default {
  components: { Pagination, firstAssessTemp, nurseList },
  data () {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10,
        orderTime: [],
        serviceTime: [],
      },
      total: 0,
      dialogVisible: false,
      dialogTit: '',
      form: {},
      ageList: [{
        label: '70岁以上',
        value: '70岁以上'
      }, {
        label: '60-70岁',
        value: '60-70岁'
      }, {
        label: '50-60岁',
        value: '50-60岁'
      }, {
        label: '40-50岁',
        value: '40-50岁'
      }, {
        label: '30-40岁',
        value: '30-40岁'
      }, {
        label: '30岁以下',
        value: '30岁以下'
      }],
      statusList: [{
        label: '待评估',
        value: '待评估'
      }, {
        label: '待派发',
        value: '待派发'
      }, {
        label: '待出发',
        value: '待出发'
      }, {
        label: '待服务',
        value: '待服务'
      }, {
        label: '服务中',
        value: '服务中'
      }, {
        label: '结束待确认',
        value: '结束待确认'
      }, {
        label: '待评价',
        value: '待评价'
      }, {
        label: '已完成',
        value: '已完成'
      }, {
        label: '已取消',
        value: '已取消'
      }, {
        label: '退款',
        value: '退款'
      },],
      selectUid: '',
      evaluateUid: '',
      firstAssessDialog: false,
      orderNum: '',
      nurseDialog: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      this.searchForm.orderStartTime = this.searchForm.orderTime[0]
      this.searchForm.orderEndTime = this.searchForm.orderTime[1]
      this.searchForm.serviceStartTime = this.searchForm.serviceTime[0]
      this.searchForm.serviceEndTime = this.searchForm.serviceTime[1]
      this.searchForm.hospitalUid = localStorage.getItem('hospitalUid')
      this.searchForm.paymentStatus = '已支付'
      let param = Object.assign({}, this.searchForm)
      delete param.orderTime
      delete param.serviceTime
      orderManagement.queryByPage(param).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })
    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10,
        orderTime: [],
        serviceTime: [],
      }
    },
    firstAssess (row) {
      this.orderNum = row.orderNum
      this.selectUid = row.uid
      let param = {
        evaluationType: "首次评估",
        hospitalUid: localStorage.getItem('hospitalUid')
      }
      orderManagement.queryEvaluation(param).then(response => {
        this.evaluateUid = response.data.rows[0]['uid']
        this.firstAssessDialog = true
      }).catch(error => {
        console.log(error)
      })
    },
    closeFirstAssess (data) {
      let resultDOReqs = []
      data.map(item => {
        if (item.questionType === '多选') {
          item.result = JSON.stringify(item.result)
        }
        resultDOReqs.push({
          questionUid: item.uid,
          questionName: item.questionName,
          orderNum: this.orderNum, //订单ID
          evaluationUid: this.selectUid, //评估表ID
          memberUid: localStorage.getItem('hospitalUid'), //做答者Id
          memberName: localStorage.getItem('hospitalAme'),
          questionType: item.questionType,
          result: item.result
        })
      })
      let param = {
        uid: this.selectUid,
        orderNum: this.orderNum,
        //operationId: localStorage.getItem('hospitalUid'), //操作人身份
        operatorName: localStorage.getItem('hospitalAme'), //操作人姓名
        resultDOReqs: resultDOReqs
      }
      orderManagement.insertBatch(param).then(response => {
        this.firstAssessDialog = false
        this.getList()
      }).catch(error => {
        console.log(error)
      })
    },
    showNurse (data) {
      this.selectUid = data.uid
      this.orderNum = data.orderNum
      this.nurseDialog = true
    },
    closeNurse (data) {
      let param = {
        uid: this.selectUid,
        orderNum: this.orderNum,
        dispatchNursesUid: data.uid,
        dispatchNurses: data.nurseName,
        nursesPhone: data.tel,
        operationId: localStorage.getItem('hospitalUid'),
        operatorName: localStorage.getItem('hospitalAme')
      }
      orderManagement.distributeNurse(param).then(result => {
        this.nurseDialog = false
        this.getList()
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        orderManagement.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    goDetails (data) {
      this.$router.push({
        name: 'orderDetails',
        query: {
          uid: data.uid,
          orderNum: data.orderNum,
          status: data.status
        }
      })
    },
    derive () {
      let url = process.env.VUE_APP_BASE_API + "/orderInfo/exportOrderInfo?"
      for (let key in this.searchForm) {
        if (this.searchForm[key]) {
          url = url + key + '=' + this.searchForm[key] + '&'
        }
      }
      //uidListStr
      let selection = this.$refs.table.selection
      let uidList = []
      if (selection.length > 0) {
        selection.forEach(item => {
          uidList.push(item.uid)
        })
        url = url + 'uidListStr=' + uidList.join(',')
      }
      // 创建一个a标签元素
      const downloadLink = document.createElement('a');

      // 设置a标签的href属性为下载链接
      downloadLink.href = url
      // 设置a标签的download属性为文件名
      downloadLink.download = 'file.pdf';
      // 将a标签添加到HTML文档中的某个元素中
      document.body.appendChild(downloadLink);
      // 触发a标签的点击事件，即模拟用户点击下载链接
      downloadLink.click();
      document.body.removeChild(downloadLink);
    },
  }
}
</script>
